package co.salehi.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;
import javax.validation.constraints.NotNull;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.entity.RooEntity;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooEntity(finders = { "findTyresByNameLike" })
public class Tyre {
	
	@NotNull
	private Integer count;

	@NotNull
	@ManyToOne
	private TyreMake brand;

	@NotNull
	private String design;

	private Boolean secondHand;

	@NotNull
	private Integer buyPrice;

	private Integer sellPrice;

	private Integer receiptPrice;

	@NotNull
	@DateTimeFormat(pattern = "dd-MM-yyyy")
	private Date insertDate = new Date();

	@DateTimeFormat(pattern = "dd-MM-yyyy")
	private Date sellDate;

	@NotNull
	private String name;

	public String toString() {
		String str = name + " " + brand.getName() + " " + brand.getCountry();
		if (sellPrice != null)
			str += ":" + sellPrice;
		return str;
	}

	public boolean equals(Object obj) {
		if (obj instanceof Tyre)
			return getId().equals(((Tyre) obj).getId());
		else
			return false;
	}

	public static List<Tyre> findAllTyres() {
		if (Country.countCountrys() == 0 || TyreMake.countTyreMakes() == 0)
			return new ArrayList<Tyre>();

		return entityManager().createQuery(
				"select o from Tyre o where o.sellDate is null", Tyre.class)
				.getResultList();
	}

	public static List<Tyre> findTyreEntries(int firstResult, int maxResults) {
		if (Country.countCountrys() == 0 || TyreMake.countTyreMakes() == 0)
			return new ArrayList<Tyre>();

		return entityManager()
				.createQuery("select o from Tyre o where o.sellDate is null ",
						Tyre.class).setFirstResult(firstResult)
				.setMaxResults(maxResults).getResultList();
	}

	public static TypedQuery<Tyre> findTyresByNameLike(String name) {
		if (name == null || name.length() == 0)
			throw new IllegalArgumentException("The name argument is required");
		name = name.replace('*', '%');
		if (name.charAt(0) != '%') {
			name = "%" + name;
		}
		if (name.charAt(name.length() - 1) != '%') {
			name = name + "%";
		}
		EntityManager em = Tyre.entityManager();
		TypedQuery<Tyre> q = em
				.createQuery(
						"SELECT Tyre FROM Tyre AS tyre WHERE LOWER(tyre.name) LIKE LOWER(:name) and tyre.sellDate is null",
						Tyre.class);
		q.setParameter("name", name);
		return q;
	}

	public static long countTyres() {
		return entityManager().createQuery(
				"select count(o) from Tyre o where o.sellDate is null ",
				Long.class).getSingleResult();
	}

	public static Object findSoldTyreEntries(int firstResult, int maxResults) {
		if (Country.countCountrys() == 0 || TyreMake.countTyreMakes() == 0)
			return new ArrayList<Tyre>();

		return entityManager()
				.createQuery("select o from Tyre o where o.sellDate is not null ",
						Tyre.class).setFirstResult(firstResult)
				.setMaxResults(maxResults).getResultList();
	}

	public static float countSoldTyres() {
		return entityManager().createQuery(
				"select count(o) from Tyre o where o.sellDate is not null ",
				Long.class).getSingleResult();
	}

	public static Object findAllSoldTyres() {
		if (Country.countCountrys() == 0 || TyreMake.countTyreMakes() == 0)
			return new ArrayList<Tyre>();

		return entityManager()
				.createQuery(
						"select o from Tyre o where o.sellDate is not null",
						Tyre.class).getResultList();
	}

}
